package com.yihong.userserver.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yihong.common.util.AjaxJson;
import com.yihong.common.util.AjaxJsonFactory;
import com.yihong.common.util.PageResult;
import com.yihong.userserver.dao.UserDao;
import com.yihong.userserver.entity.User;
import com.yihong.userserver.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName UserService
 * @Description
 * @Author 孟志浩
 * @Date 2019/4/2 9:54
 **/
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserDao userDao;

    /**
      * 分页查询
      * @param page         页码
      * @param size         每页数量
      * @param words        查询关键字
      * @param isValid      是否启用
      * @return com.yihong.common.util.AjaxJson
      * @author 孟志浩
      * @date 2019/4/2
      **/
    public AjaxJson findAll(int page, int size, String words, int isValid){
        if(size == 0)
        {
            return AjaxJsonFactory.createAjaxJson(500, "请求参数异常");
        }
        try {
            Page<User> userPage = PageHelper.startPage(page, size);
            List <User> users = userDao.findAll(words, isValid);
            for (int i = 0; i < users.size(); i++) {
                users.get(i).setRoleList(null);
            }
            PageResult pageResult = new PageResult(userPage.getTotal(), userPage.getResult());
            return AjaxJsonFactory.createOnlyData(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJsonFactory.createAjaxJson(500, e.getMessage());
        }
    }

    /**
      * 根据id查询
      * @param id 对象id
      * @return com.yihong.common.util.AjaxJson
      * @author 孟志浩
      * @date 2019/4/2
      **/
    public AjaxJson get(long id){
        return AjaxJsonFactory.createOnlyData(userRepository.getOne(id));
    }

    /**
      * 添加用户
      * @param user         用户实体
      * @param createId     创建人id
      * @return com.yihong.common.util.AjaxJson
      * @author 孟志浩
      * @date 2019/4/2
      **/
    public AjaxJson addUser(User user, long createId){
        user.setCreatorId(createId);
        user.setCreateTime(new Date());
        user.setIsValid(1);
        try {
            userRepository.saveAndFlush(user);
            return AjaxJsonFactory.createOK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJsonFactory.createAjaxJson(500,e.getMessage());
        }
    }

    /**
      * 更新用户
      * @param user         用户
      * @param updatorId    创建人
      * @return com.yihong.common.util.AjaxJson
      * @author 孟志浩
      * @date 2019/4/2
      **/
    public AjaxJson updateUser(User user, Long updatorId) {
        try {
            user.setModifyTime(new Date());
            user.setModifierId(updatorId);
            userRepository.saveAndFlush(user);
            return AjaxJsonFactory.createAjaxJson("修改成功");
        }catch (Exception e)
        {
            return AjaxJsonFactory.createAjaxJson(500, "修改失败" + e.getMessage());
        }
    }

}
